package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    // 根据环信用户id,查询用户详情
    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        // 根据环信id查询用户
        User user = userApi.findByHuanXin(huanxinId);
        // 根据用户id查询用户详情
        UserInfo userInfo = userInfoApi.findById(user.getId());
        // 封装为vo对象,拷贝一下
        UserInfoVo vo = new UserInfoVo();
        // copy同名同类型的属性
        BeanUtils.copyProperties(userInfo,vo);
        if (userInfo.getAge() != null){
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    // 添加好友关系
    public void contacts(Long friendId) {
        // 1:将好友关系注册到环信
        Boolean addContact = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(),
                Constants.HX_USER_PREFIX + friendId);
        if (!addContact){
            throw new BusinessException(ErrorResult.error());
        }
        // 2:如果注册成功,将好友关系记录到mongoDb中
        friendApi.save(UserHolder.getUserId(),friendId);
    }


    // 分页查询好友列表
    public PageResult findContacts(Integer page, Integer pagesize, String keyword) {
        // 1:调用api查询当前用户的好友数据  -- list<Friend>
        List<Friend> list = friendApi.findByUserId(UserHolder.getUserId(),page,pagesize);
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
        // 2:提取数据列表中的好友id
        List<Long> friendId = CollUtil.getFieldValues(list, "friendId", Long.class);
        // 3:根据好友id查询好友的详细信息
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> map = userInfoApi.findByIds(friendId, userInfo);
        // 4:构造vo对象
        List<ContactVo> vos = new ArrayList<>();
        for (Friend friend : list) {
            UserInfo userInfo1 = map.get(friend.getFriendId());
            if (userInfo1 != null){
                ContactVo vo = ContactVo.init(userInfo1);
                vos.add(vo);
            }
        }
        // counts = 总数(0L)
        return new PageResult(page,pagesize,0L,vos);
    }
}
